package niumao.minecraft.uhc.listener;


import niumao.minecraft.uhc.Main;
import niumao.minecraft.uhc.Util;
import org.bukkit.ChatColor;
import org.bukkit.Sound;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Arrow;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.entity.ProjectileHitEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class modular implements Listener {

    @EventHandler
    public void on(PlayerInteractEvent event) {
        if (event.getHand() == EquipmentSlot.HAND) {
            if (event.getAction() == Action.LEFT_CLICK_AIR) {
                Player player = event.getPlayer();
                ItemStack item = player.getInventory().getItemInMainHand();
                ItemMeta itemMeta = item.getItemMeta();
                if (itemMeta == null) return;
                if (itemMeta.getDisplayName().equals(Util.getStr("Modular"))) {
                    int i = itemMeta.getCustomModelData();
                    if (i == 3) {
                        i = 1;
                        itemMeta.addEnchant(Enchantment.ARROW_KNOCKBACK, 1, true);
                        if (Main.getLang().equalsIgnoreCase("en_us"))
                            item.setItemMeta(change(i, itemMeta, "Punch", player));
                        else item.setItemMeta(change(i, itemMeta, "冲击", player));
                    } else if (i == 1) {
                        i = 2;
                        itemMeta.removeEnchant(Enchantment.ARROW_KNOCKBACK);
                        if (Main.getLang().equalsIgnoreCase("en_us"))
                            item.setItemMeta(change(i, itemMeta, "Poison", player));
                        else item.setItemMeta(change(i, itemMeta, "毒素", player));
                    } else if (i == 2) {
                        i = 3;
                        if (Main.getLang().equalsIgnoreCase("en_us"))
                            item.setItemMeta(change(i, itemMeta, "Lightning", player));
                        else item.setItemMeta(change(i, itemMeta, "雷击", player));
                    }
                }
            }
        }
    }

    @EventHandler
    public void on2(ProjectileHitEvent event) {
        if (event.getHitBlock() != null) return;
        if (!(event.getEntity().getShooter() instanceof Player)) return;
        Player player = (Player) event.getEntity().getShooter();
        ItemStack item = player.getInventory().getItemInMainHand();
        ItemMeta itemMeta = item.getItemMeta();
        if (itemMeta == null) return;
        if (itemMeta.getDisplayName().equals(Util.getStr("Modular"))) {
            if (itemMeta.getCustomModelData() == 2) {
                Arrow arrow = (Arrow) event.getEntity();
                arrow.addCustomEffect(new PotionEffect(PotionEffectType.POISON, 20, 0), true);
            }
            if (itemMeta.getCustomModelData() == 3) {
                Objects.requireNonNull(event.getHitEntity()).getWorld().strikeLightningEffect(event.getHitEntity().getLocation());
            }
        }
    }

    private ItemMeta change(int i, ItemMeta itemMeta, String mode, Player player) {
        List<String> lore = itemMeta.getLore();
        assert lore != null;

        if (Main.getLang().equalsIgnoreCase("en_us"))
            lore.set(4, ChatColor.GREEN + "Current Mode: " + ChatColor.GOLD + mode + ChatColor.DARK_GRAY + " [" + i + "]");
        else lore.set(4, ChatColor.GREEN + "当前模式: " + ChatColor.GOLD + mode + ChatColor.DARK_GRAY + " [" + i + "]");
        itemMeta.setLore(lore);

        itemMeta.setCustomModelData(i);
        player.playSound(player.getLocation(), Sound.BLOCK_TRIPWIRE_CLICK_ON, 1, 1);

        if (Main.getLang().equalsIgnoreCase("en_us"))
            player.sendMessage(ChatColor.GREEN + "Your Modular Bow current mode is "
                    + ChatColor.GOLD + mode + ChatColor.DARK_GRAY + " [" + i + "]" + ChatColor.GRAY + ".");
        else player.sendMessage(ChatColor.GREEN + "你的变换之弓当前模式为"
                + ChatColor.GOLD + mode + ChatColor.DARK_GRAY + " [" + i + "]" + ChatColor.GRAY + ".");

        return itemMeta;
    }
}
